﻿/**************************************************************
 * Copyright gt1987. All rights reserved.
 * 
 * Author: guitao(guitao@eastmoney.com) 
 * Create Date: 2020/4/14 15:19:09
 * Description: MessageProvider
 *          
 * Revision History:
 *      Date         Author               Description
 *              
***************************************************************/

using gt.dotnetcore.webapi.extensions.ApiException;
using Microsoft.Extensions.Options;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace gt.ShortMessage.Services.Impls
{
    public class MessageProvider : IMessageProivder
    {
        private readonly Dictionary<string, string> _bizTemplates = null;

        public MessageProvider(IOptionsSnapshot<Dictionary<string, string>> options)
        {
            var bizTemplates = options.Value;
            _bizTemplates = bizTemplates ?? new Dictionary<string, string>();
        }

        public string BuildShortMessage(string businessId, string code)
        {
            if (!_bizTemplates.TryGetValue(businessId, out string textTemplate))
            {
                throw new BusinessException(-8, "invalid businessId");
                //下面方式会返回 系统错误
                //throw new InvalidOperationException();
            }

            return string.Format(textTemplate, code);
        }
    }
}
